package top.saplf.sileny.reader.network

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import cn.nekocode.rxlifecycle.compact.RxLifecycleCompact
import com.jakewharton.rxbinding2.support.v4.widget.RxSwipeRefreshLayout
import com.jakewharton.rxbinding2.support.v4.widget.refreshes
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.fragment_network_article_index.*
import top.saplf.sileny.R
import top.saplf.sileny.base.ui.SilenyFragment
import top.saplf.sileny.net.gank.GankItem
import top.saplf.sileny.net.gank.GankService

/**
 * @author saplf
 */
class NetworkArticleIndexFragment : SilenyFragment() {
    companion object {
        fun getInstance(): NetworkArticleIndexFragment =
                NetworkArticleIndexFragment().apply { }

        private const val PAGE_START = 1
    }

    private val mAdapter: NetworkArticleAdapter
            by lazy {
                NetworkArticleAdapter().apply {
                    bindToRecyclerView(articleList)
                    setEmptyView(R.layout.view_loading)
                    setEnableLoadMore(true)
                    setOnLoadMoreListener({
                        loadData {
                            addData(it)
                            loadMoreComplete()
                        }
                    }, articleList)
                    itemClickAction = {
                        startActivity(Intent(context, NetworkArticleViewActivity::class.java).apply {
                            with(NetworkArticleViewActivity) {
                                uri = it.url
                                desc = it.desc
                            }
                        })
                    }
                }
            }
    private val mSize: Int = 20
    private var mPage: Int = PAGE_START

    override fun onLayoutRes(): Int = R.layout.fragment_network_article_index

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        with(articleList) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }

        swipeLayout.refreshes()
                .subscribe {
                    mPage = PAGE_START
                    loadData {
                        mAdapter.setNewData(it)
                        swipeLayout.isRefreshing = false
                    }
                }

        loadData {
            mAdapter.addData(it)
        }
    }

    private inline fun loadData(crossinline action: (List<GankItem>) -> Unit) {
        GankService.getGankService()
                .obtainCategory(mSize, mPage++)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleCompact.bind(this).withObservable())
                .subscribe({
                    if (it.error) {
                        mAdapter.setEmptyView(R.layout.view_empty)
                    } else {
                        action(it.results)
                    }
                }, {
                    it.printStackTrace()
                    mAdapter.setEmptyView(R.layout.view_empty)
                })
    }
}
